const decodeQRCode = (qrCode) => {
  const tfa = {};
  const match = qrCode.match(/^otpauth:\/\/totp\/([^:]+):([^\?]+)\?(.*)$/i);
  if (match != null) {
    tfa.site = match[1];
    tfa.email = match[2];
    const { digits, period, secret, algorithm } = decodeUrlQuery(match[3]);
    tfa.len = parseInt(digits),
    tfa.ts = parseInt(period);
    tfa.algo = algorithm;
    tfa.secret = secret;
    return tfa;
  }
  return false;
}

const decodeUrlQuery = (queryString) => {
  const params = queryString.split("&");
  const query = {};

  for (let i = 0; i < params.length; i++) {
    let arr = params[i].split("=");
    if (arr != null && arr.length == 2) {
      query[arr[0]] = arr[1];
    }
  }
  return query;
}

export default decodeQRCode;